package cn.jx.asc.lhg.bzxq.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.jx.asc.lhg.bzxq.utils.Http;
import cn.jx.asc.lhg.bzxq.R;
import cn.jx.asc.lhg.bzxq.model.UserBase;
import cn.jx.asc.lhg.bzxq.utils.SoundUtils;

import java.util.List;

public class BangDan extends Activity {
    ListView listView;
    List<UserBase> userBases;
    ProgressDialog pd;
    SoundUtils soundUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉窗口标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //使程序全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.bangdan);
        Toast.makeText(this,"点击头部返回",Toast.LENGTH_SHORT).show();
        initview();
    }
    public void initview(){
        listView = (ListView) findViewById(R.id.bangdan_listview);
        pd = new ProgressDialog(this);
        pd.setMessage("获取中...");
        pd.show();
        //开启子线程获取榜单信息
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String path = "GetName";
                    userBases = Http.getUser(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(0x123);
            }

        }).start();
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            pd.dismiss();
            if(msg.what==0x123){
                if(userBases!=null&&userBases.size()>0){
                    listView.setAdapter(new MyAdapter());
                }else{
                    Toast.makeText(BangDan.this,"获取失败,请检查你的网络!",Toast.LENGTH_SHORT).show();
                }
            }
        }
    };

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return userBases.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(BangDan.this,R.layout.item_bangdan_view,null);
            TextView textView = (TextView) convertView.findViewById(R.id.bd_name);
            TextView textView1 = (TextView) convertView.findViewById(R.id.bd_fen);
            TextView textView2 = (TextView) convertView.findViewById(R.id.bd_mingci);
            textView.setText(userBases.get(position).getName());
            textView1.setText(userBases.get(position).getFen());
            textView2.setText(position+1+"");
            return convertView;
        }
    }
    public void fan(View v){
        //开始播放音效
        soundUtils = new SoundUtils(BangDan.this, SoundUtils.RING_SOUND);
        soundUtils.putSound(0, R.raw.btn_yin);
        soundUtils.playSound(0, SoundUtils.SINGLE_PLAY);
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(soundUtils!=null){
            soundUtils.stop();
        }

    }
}
